﻿using System;
using System.Collections.Generic;
using System.Linq;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using YArchitech.Revit;
using YJKCopyModel.QuickModelFrame;

namespace YJKCopyModel.RoomTransModel
{
	[Rebuilder(RebuilderName = "RoomRebuilder")]
	public class RoomRebuilder : IRevitModelRebuilder
	{
		public bool TryRebuildRevitModel(Autodesk.Revit.DB.Document inputDoc, RevitModelRequest inputReqeust, out BuildData buildData)
		{
			buildData = new RoomBuildData();
			buildData.NeedResponse = true;
			Transaction transaction = new Transaction(inputDoc, RoomDefaultSet.TransformNameCreateRoom);
			try
			{
				transaction.Start();
				RoomRevitRequest roomRevitRequest = inputReqeust as RoomRevitRequest;
				XYZ xyz = (roomRevitRequest.Text.ActualBox[2] + roomRevitRequest.Text.ActualBox[1]) / 2.0;
				Room room = inputDoc.GetRoomAtPoint(xyz);
				bool flag = room == null;
				if (flag)
				{
					room = this.CreateRoom(inputDoc, xyz);
					bool flag2 = room == null;
					if (flag2)
					{
						return false;
					}
					(buildData as RoomBuildData).HasCreatedRoom = true;
					room.Name = roomRevitRequest.Text.UseText;
					RoomManager.AddRoomShareParameters(room, roomRevitRequest.UseExternalCommandData.Application);
					RoomTag roomTag = this.CreateRoomTag(inputDoc, room, xyz, roomRevitRequest.ConfigInfo.ThisRoomTagType);
				}
				else
				{
					TextNote textNode = inputDoc.GetTextNode(inputDoc.ActiveView, xyz, XYZ.BasisX, XYZ.BasisY, (double)RoomDefaultSet.TextNodeLineWidth, (Autodesk.Revit.DB.TextAlignFlags)64, roomRevitRequest.Text.UseText);
					this.ResetTextNodeType(inputDoc, textNode);
					buildData.CreatedElement = textNode;
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				bool flag3 = transaction.HasStarted();
				if (flag3)
				{
					transaction.RollBack();
				}
				return false;
			}
			return true;
		}

		private Room CreateRoom(Autodesk.Revit.DB.Document doc, XYZ ptPos)
		{
			return doc.Create.NewRoom(doc.ActiveView.GenLevel, new UV(ptPos.X, ptPos.Y));
		}

		private RoomTag CreateRoomTag(Autodesk.Revit.DB.Document doc, Room room, XYZ ptPos, RoomTagType roomTagType)
		{
			UV point = new UV(ptPos.X, ptPos.Y);
			RoomTag roomTag = RevitVersionFuncs.NewRoomTag(doc, room, point, doc.ActiveView);
			bool flag = roomTag != null;
			if (flag)
			{
				roomTag.RoomTagType = roomTagType;
			}
			return roomTag;
		}

		private void ResetTextNodeType(Autodesk.Revit.DB.Document doc, TextNote textNote)
		{
			try
			{
				bool flag = textNote.TextNoteType.Name == RoomDefaultSet.RoomNameTextNoteType;
				if (!flag)
				{
					IEnumerable<TextNoteType> source = YJKElementFilter.GetElementByClass(typeof(TextNoteType), doc).Cast<TextNoteType>();
					TextNoteType textNoteType = source.FirstOrDefault((TextNoteType item) => item.Name == RoomDefaultSet.RoomNameTextNoteType);
					bool flag2 = textNoteType == null;
					if (flag2)
					{
						textNoteType = (textNote.TextNoteType.Duplicate(RoomDefaultSet.RoomNameTextNoteType) as TextNoteType);
						Parameter parameter = textNoteType.GetParameter(BuiltInParameter.LINE_COLOR);
						parameter.Set(255);
					}
					textNote.TextNoteType = textNoteType;
				}
			}
			catch (Exception)
			{
			}
		}
	}
}
